<template>
  <view class="address">
    <!-- 未选择收货地址时 -->
    <view class="choose-box" v-if="Object.keys(address).length === 0">
      <button type="primary" size="mini" class="chooseBtn" @click="goToChoose">请选择收货地址+</button>
    </view>
    <!-- 选择了收货地址时 -->
    <view class="address-info-box" v-else @click="goToChoose">
      <view class="row1">
        <view class="row1-left">
          <view class="username">收货人：<text>{{ address.userName }}</text></view>
        </view>
        <view class="row1-right">
          <view class="phone">电话：<text>{{ address.telNumber }}</text></view>
          <uni-icons type="right" size="16"></uni-icons>
        </view>
      </view>
      <view class="row2">
        <view class="row2-left">收货地址：</view>
        <view class="row2-right">{{ addstr }}</view>
      </view>
    </view>
    <!-- 底部边框线 -->
    <image src="/static/address-line.png" class="address-line" mode="aspectFill"></image>
  </view>
</template>

<script setup>
  import { computed, watch } from 'vue';
  import { onLoad } from '@dcloudio/uni-app';
  import { useStore } from 'vuex';
  
  const store = useStore();
  
  const address = computed(() => store.state.user.address);
  const addstr = computed(() => store.getters['user/addstr']);

  function goToChoose() {
    uni.chooseAddress({
      success(res) {
        if(res.errMsg === 'chooseAddress:ok') {
          store.commit('user/saveAddress', res);
        }
      },
      fail(err) {
        console.log('fail', err);
        // 安卓手机和ios手机的未授权提示不一样,前为安卓,后为ios
        if(err.errMsg === 'chooseAddress:fail auth deny' || err.errMsg === 'chooseAddress:fail authorize no response') {
          // 重新授权
          reAuth();
        }
      }
    })
  }
  
  function reAuth() {
    uni.showModal({
      content: '检测到您没打开地址权限，是否去设置打开？',
      success: ({confirm, cancel}) => {
        if(cancel) {
          uni.$showMsg('取消了授权');
        }
        if(confirm) {
          uni.$showMsg('去授权');
          uni.openSetting({
            success: (res) => {
              if(res.authSetting['scope.address']) {
                uni.$showMsg('授权成功，请选择地址！');
              } else {
                uni.$showMsg('您取消了地址授权！');
              }
            },
            fail: (err) => {
              console.log('设置接口调用失败', err);
            }
          })
        }
      },
      fail: (err) => {
        console.log('弹窗接口调用失败');
      }
    })
  }
</script>

<style lang="scss" scoped>
  .choose-box {
    height: 90px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .address-info-box {
    font-size: 12px;
    height: 90px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    padding: 0 5px;
    .row1 {
      display: flex;
      justify-content: space-between;
      .row1-right {
        display: flex;
        justify-content: space-between;
      }
    }
    .row2 {
      display: flex;
      align-items: center;
      margin-top: 10px;
      .row2-left {
        white-space: nowrap;
      }
    }
  }
  .address-line {
    display: block;
    width: 100%;
    height: 5px;
  }
</style>